import os
import uuid

"""
使用 uuid 重命名文件生成唯一文件名
"""
def rename_file_by_uuid(folder_path, custom_name):
    """
    :param folder_path:
    :param custom_name:
    :return:
    """
    files = os.listdir(folder_path)
    filter_files = [file for file in files if os.path.splitext(file)[1] == ".xlsx"
                    and os.path.isfile(folder_path + file)]
    for i, file_name in enumerate(filter_files):
        _, file_extension = os.path.splitext(file_name)
        if os.path.exists(folder_path + file_name) and os.path.isfile(folder_path + file_name) and file_extension == '.xlsx':
            uuid_file_name = uuid.uuid4()
            uuid_file_name = f"{uuid_file_name}-{i + 1}{file_extension}"
            os.rename(folder_path + file_name, folder_path + uuid_file_name)
            # TODO 这里是功能测试点 可以进行单点测试
            print(i + 1, "\t", file_name, "\t", uuid_file_name)
    rename_file_by_custom(folder_path, custom_name)

"""
使用自定义命名格式批量命名文件
"""
def rename_file_by_custom(folder_path, custom_name):
    """
    :param folder_path:
    :param custom_name:
    :return:
    """
    files = os.listdir(folder_path)
    filter_files = [file for file in files if os.path.splitext(file)[1] == ".xlsx"
                    and os.path.isfile(folder_path + file)]
    for i, file_name in enumerate(filter_files):
        _, file_extension = os.path.splitext(file_name)
        if os.path.exists(folder_path + file_name) and os.path.isfile(folder_path + file_name) and file_extension == '.xlsx':
            new_file_name = f"{custom_name}-{i + 1}{file_extension}"
            os.rename(folder_path + file_name, folder_path + new_file_name)
            # TODO 这里是功能测试点 可以进行单点测试
            print(i + 1, "\t", file_name, "\t", new_file_name)

# 入口 main 函数
if __name__ == '__main__':
    folder_path = "/Users/lvwenhao/Downloads/ScriptWorker/ScriptWorker-1/"
    custom_name = "2024年7月财务报表分表文件"
    rename_file_by_uuid(folder_path, custom_name)